// challeng/pages/zplay/zplay.js
var app = getApp();
var answerTimer = null;
Page({

  data: {
	animationLine:{},
	animationLineData:{},
	animationRen:{},
	animationRenData:{},
	animationBg:{},
	animationBgData:{},
	  
	game_bg:'',
	gametoken:'',
	openid:'',
	mem:{},
	total:0,
	cur_num:0,
	times:[],
	g_nums:[],
	answerIng:false,
	beginCountDown: 3,//游戏开始--倒计时
	gamingStatus: 'notBegin',//游戏状态4个。notBegin未开始，running正在运行，fail挑战失败,success挑战成功
	
	rpxTopx:0,//1像素等于多少rpx
	l_width:50,//左边柱子宽度
	m_left:0,//两个柱子之间的距离，最小10rpx,最大400rpx;
	L_width:10,//桥初始宽度
	L_height:60,//桥初始高度
	long_size:0,//长按没100毫秒的大小变换
	imgs:[],//间隙和柱子宽度
	b_left:0,
	pics:{},
	monkey:'',

  },

  onLoad: function (options) {
	  var that = this;
	  wx.showLoading({title:'数据加载中...'});
	  app.sysInit(this,function(res){
		  app.getUserInfo(function (res) {
			  var openid = res['openid'];
			  //获取分享内容
			  app.getShareData(function(res){
				  that.setData({shareData:res.data});
			  },openid,9);
			  
			  that.setData({openid:openid});
			  app.util.request({
				  url: 'entry/wxapp/zplay',
				  data: {
					  m: 'junsion_challeng',
					  t: 1,
					  openid:openid,
					  game:9,
				  },
				  success: function (res) {
					  wx.hideLoading();
					  if(res.data.code==0 || res.data.code==2){
						  var code = res.data.code;
						  wx.showModal({
							  title:res.data.msg,
							  showCancel:false,
							  success: function(res) {
								  wx.showLoading({title:'数据加载中...'});
								  if(code==2)
									  var url = '../index/index?gobuy=1';
								  else var url = '../index/index';
								  wx.redirectTo({
									  url: url
								  })
							  }
						  });
					  }
					  else{
						  that.setData({
							  game_bg:res.data.game_bg,
							  gametoken:res.data.gametoken,
							  mem:res.data.mem,
							  pics:res.data.pics,
							  monkey:res.data.pics.monkey,
							  beginCountDown:res.data.down,
							  total:res.data.total,
							  g_nums:res.data.g_nums,
							  times:res.data.times,
							  answerIng:false,
						  });
						  
						  var sys_times = res.data.times;
						  wx.getSystemInfo({
					          success:function(res){
					        	  var windowWidth = res.windowWidth;
					        	  var rpxTopx = 750 / parseInt(windowWidth);
					        	  that.setData({
					        		  rpxTopx:rpxTopx,
								  });
					        	  
					        	  if(parseInt(sys_times[0])>0){
					        		  var p_width = parseInt(sys_times[0]) * rpxTopx;
								  }
								  else if(parseInt(sys_times[1])>0){
									  var p_width = parseInt(sys_times[1]) * rpxTopx;
								  }
								  else if(parseInt(sys_times[2])>0){
									  var p_width = parseInt(sys_times[2]) * rpxTopx;
								  }
								  else{
									  var p_width = 10 * rpxTopx;
								  }
					        	  var m_left = app.fRandomBy(60,500);
					        	  var imgs = that.data.imgs;
					        	  imgs.push({p_width:p_width,m_left:m_left});
								  that.setData({
									  imgs:imgs
								  });
					          }
					      });
						  that.beginCountDown(res.data.down);//游戏主入口
					  }
				  },
			  });
		  });
	  });
	  var music = wx.getStorageSync('music');
      that.setData({
		  music:music.music,
		  err_music:music.err_music,
	  });
  },
  onUnload: function(){
  	  clearInterval(answerTimer);
  },
  onReady: function () {
	  var that=this;
  	  this.audioCtx = wx.createAudioContext('myAudio')
      this.errAudio = wx.createAudioContext('errAudio')
      this.audioCtx.play();
      
  	  //动画
  	  var animationLine = wx.createAnimation({
  		  duration:1000,
  		  timingFunction:"ease",
  		  transformOrigin:"0 100% 0"
  	  })
  	  //猴子动画
  	  var animationRen = wx.createAnimation({
  		  duration:1000,
  		  timingFunction:"linear",
  		  transformOrigin:"0 100% 0"
  	  })
    var animationBg = wx.createAnimation({
  	    duration:1000,
  	    timingFunction:"linear",
  	    transformOrigin:"0 100% 0"
  	})
  	that.setData({
  		animationLine:animationLine,
  		animationRen:animationRen,
  		animationBg:animationBg,
	});
  },
  
  longTap:function(e){
	  var that=this;
	  var animation = that.data.animationLine;
	  answerTimer = setInterval(function () {
			var long_size = parseFloat(that.data.long_size);
			long_size = long_size + 0.3;
			var real_size = 1 + long_size;
			animation.scaleY(real_size).step({timingFunction:"ease"});
		  that.setData({
			  animationLineData:animation.export(),
			  long_size:long_size,
		  })
  	}, 130);
	  
  },
  longEnd:function(e){
	  var that=this;
	  var long_end = e.timeStamp;
	  var long_size = parseFloat(that.data.long_size);
	  var cur_num = parseInt(that.data.cur_num);
	  var pics = that.data.pics;
	  var imgs = that.data.imgs;
	  var b_left = parseFloat(that.data.b_left);
	  imgs = imgs[cur_num];
	  
	  clearInterval(answerTimer);//清除定时器
	  //执行桥动画
	  var animation = that.data.animationLine;
	  animation.translateX(b_left).scaleX(long_size).scaleY(1).rotate(90).step({timingFunction:"step-start",duration:400});
	  that.setData({
    	  animationLineData:animation.export(),
    	  long_size:0
      })
	  
      var m_left = parseInt(imgs.m_left);//间隙
	  var p_width = parseInt(imgs.p_width);//柱子的宽度
	  var real_m_left = 60 * long_size;//实际高度
	  var sure_left = m_left + p_width;//正确走的路程
	  
	  var rpxTopx = parseFloat(that.data.rpxTopx);
	  //获取猴子移动的距离
	  var move_size = sure_left / rpxTopx;
	  var animationRen = that.data.animationRen;
	  if(parseFloat(real_m_left)>=parseFloat(m_left) && parseFloat(real_m_left)<=parseFloat(sure_left)){
		  var move_size = sure_left / rpxTopx;
	  }
	  else{
		  var move_size = real_m_left / rpxTopx;
	  }
	  var b_left = b_left + move_size;
	  //执行猴子动画
	  that.setData({
    	  monkey:pics.monkey1,
      })
	  setTimeout(function(){
		  animationRen.translateX(b_left).step();
		  that.setData({
			  animationRenData:animationRen.export(),
		  })
	  },400);
	  
	  if(parseFloat(real_m_left)>=parseFloat(m_left) && parseFloat(real_m_left)<=parseFloat(sure_left)){
		  cur_num++;
		  if (cur_num == parseInt(that.data.total)) {
			that.audioCtx.pause()
			app.checkReward(that,9,cur_num);//中奖了
			return false;
		  }
		  //在过桥范围内
		  var animationBg = that.data.animationBg;
		  //执行背景动画  
		  setTimeout(function(){
			  animationBg.translateX(-b_left).step();
			that.setData({
		    	  animationBgData:animationBg.export(),
		    })
		    //执行桥动画  
		    setTimeout(function(){
		    	animation.scaleX(1).scaleY(1).rotate(0).translateX(b_left).step();
				that.setData({
			    	  animationLineData:animation.export(),
			    	  b_left:b_left,
			    })
		    },1000);
			setTimeout(function(){
				that.setData({
		    	  monkey:pics.monkey,
		        });
			},1050);
		    
			var p_width = 0;
			if(parseInt(that.data.g_nums[0])>cur_num){
				p_width = parseInt(that.data.times[0]) * rpxTopx;
			}
			else if(parseInt(that.data.g_nums[1])>cur_num){
				p_width = parseInt(that.data.times[1]) * rpxTopx;
			}
			else if(parseInt(that.data.g_nums[2])>cur_num){
				p_width = parseInt(that.data.times[2]) * rpxTopx;
			}
			else{
				p_width = 10 * rpxTopx;
			}
		    var m_left = app.fRandomBy(60,500);
			  var imgs = that.data.imgs;
			  imgs.push({p_width:p_width,m_left:m_left});
			  that.setData({
				  imgs:imgs,
				  L_width:10,
				  L_height:60,
				  cur_num:cur_num,
			  });
		    
		  },400);
	  }
	  else{
		  //挑战失败
		setTimeout(function(){
			animationRen.rotate(-180).translateX(b_left).translateY(-150).step();
			that.setData({
		    	  animationRenData:animationRen.export(),
		    })
		    animation.scaleX(1).scaleY(long_size).rotate(180).step({timingFunction:"step-start",duration:400});
			that.setData({
				animationLineData:animation.export(),
			})
		    that.audioCtx.pause()
		    app.tzfail(that,9);//挑战失败
	    },1300);
	  }
  },
  
  //游戏开始倒计时
  beginCountDown:function(countTime){
      var that=this;
      var timer = setInterval(function () {
          that.setData({ beginCountDown: --countTime });
          if (countTime <= 0) {
              clearInterval(timer);
              that.setData({
                  gamingStatus:'running'
              });
              wx.showToast({
            	  title:"长按屏幕，使桥变长",
            	  icon:'none',
            	  duration:1500,
            	  mask:true,
              });
          }
      }, 1000)
  },
})